package com.etc.service.arithmetic;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

/**
 * 调用百度地图API获取酒店的详细信息
 * @author Administrator
 *
 */
public class HotelRecommend {
	
	/**
	 * 根据景点信息获取附近1500米之内的酒店信息
	 * @param scenicX	景点纬度
	 * @param scenicY	景点经度
	 * @param sort_rule	1：经济型；0：豪华型
	 * @return	返回该酒店的具体信息
	 * @throws JSONException
	 */
	public static HotelJson  getHotelMessage(double scenicX,double scenicY, int sort_rule) throws JSONException {
		String url = "http://api.map.baidu.com/place/v2/search?query=hotel&location="+scenicX+","+scenicY+"&radius=1500&scope=2&page_size=1&output=json&ak=jE58U0IbCNrx2LHDg4XgNwNNnsigpg96&filter=industry_type:hotel|sort_name:price|sort_rule:"+sort_rule;				
		
		String json = connectURL(url);
		System.out.println(json);
		JSONObject jsonText = new JSONObject(json);
		Type type = new TypeToken<List<HotelJson>>() {}.getType();
		List<HotelJson> hotelLists = new Gson().fromJson(jsonText.getString("results"), type);
		HotelJson hj =null; 
		if(hotelLists.size()>0)
		{
			hj = hotelLists.get(0);
		}
		//System.out.println("酒店名字："+hj.getName()+",价格："+hj.getDetail_info().getPrice());
		return hj;
	}
	
	//java内部调用浏览器获取JSON格式的信息
	public static String connectURL(String dest_url) {
        String rec_string = "";
        URL url = null;
        HttpURLConnection urlconn = null;
        OutputStream out = null;
        BufferedReader rd = null;
        try {
            url = new URL(dest_url);
            urlconn = (HttpURLConnection) url.openConnection();
            urlconn.setReadTimeout(1000 * 30);
            urlconn.setRequestMethod("GET");
            urlconn.setDoInput(true); 
            urlconn.setDoOutput(true);
            out = urlconn.getOutputStream();
           // out.write(commString.getBytes("UTF-8"));
            out.flush(); 
            out.close();
            rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(),"UTF-8"));
            StringBuffer sb = new StringBuffer();
            int ch;
            while ((ch = rd.read()) > -1)
                sb.append((char) ch);
            rec_string = sb.toString();
        } catch (Exception e) {

            return "";
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (urlconn != null) {
                    urlconn.disconnect();
                }
                if (rd != null) {
                    rd.close();
                }
            } catch (Exception e) {

            }
        }
        return rec_string;
    }


}
